class WeaponListUI extends eui.ItemRenderer{
    private main_group:eui.Group;
    private oldData:any;
    private isAllHave:boolean = false;
    private timer:egret.Timer;
    private count:number = 100;
    private randomList = [];
    private call:Function;
    private scope;
    private lastItem:WeaponSmallItem;
    constructor()
    {
        super();
        this.skinName = 'WeaponListView';
    }

    public childrenCreated()
    {
        super.childrenCreated();
        if(!this.timer)
        this.timer = new egret.Timer(this.count);
        this.timer.addEventListener(egret.TimerEvent.TIMER, this.update, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.clear, this);
    }

    protected update()
    {
        if(this.randomList.length <= 0){
            this.timer.stop();
            if(this.call)
            {
                let parma = this.lastItem.getData();
                this.call.call(this.scope, parma);
                this.lastItem.scaleX = this.lastItem.scaleY = 1;
                this.lastItem.setSelectState();
                RoleCache.instance().curHadWeapon.push(parma.weaponData.id+'');
                this.lastItem.update();
            }
            return;
        }
        else
        {
            let index = this.randomList.shift();
            let item:WeaponSmallItem = this.main_group.getChildByName('item_'+(index+1)) as WeaponSmallItem;
            if(item)
            {
                if(this.lastItem)
                {
                    this.lastItem.scaleX = this.lastItem.scaleY = 1;
                    this.lastItem.img_select.visible = false;
                }
                item.scaleX = item.scaleY = 1.1;
                item.img_select.visible = true;
                this.lastItem = item;
                WeChatgGameManager.instance().vibrateShort();
            }
        }
    }

    public dataChanged()
    {
        if(!this.data || this.data == this.oldData)return;
        this.oldData = this.data;
        this.updateUI();
    }

    public get da()
    {
        return this.oldData;
    }

    public showRandom(data, call:Function, scope)
    {
        let randomList = data.randomList;
        let weaponId = data.weaponId;
        let list = this.data.good_list.split(',');
        let aimIndex = list.indexOf(weaponId+'');

        if(randomList <= 1)
        {
            this.randomList = randomList;
        }
        else
        {
            for(let v of randomList){
                this.randomList.push(v);
            }
            if(this.randomList.length < 9)
            {
                while(this.randomList.length < 9)
                {
                    let index = Math.floor(Math.random() * randomList.length);
                    let v = randomList[index];
                    this.randomList.push(v);
                }
                this.randomList.push(aimIndex);
            }
            else
            {
                this.randomList.push(aimIndex);
            }
        }
        
        this.call = call;
        this.scope = scope;
        this.timer.start();
    }

    protected updateUI()
    {
        let list = this.data.good_list.split(',');
        for(let index = 0;index<9;index++)
        {
            let item:WeaponSmallItem = this.main_group.getChildByName('item_'+(index+1)) as WeaponSmallItem;
            if(list[index])
            {
                let id = parseInt(list[index]);
                let v = WeaponTpl.instance().getDataByWeaponId(id);
                let clone = JSON.parse(JSON.stringify(v));
                let i = index;
                if(i == 0 && this.itemIndex == 0)
                    clone.index = 1;
                else
                    clone.index = null;
                if(!item)
                {
                    item = new WeaponSmallItem();
                    item.anchorOffsetX = item.width >> 1;
                    item.anchorOffsetY = item.height >> 1;
                    item.name = 'item_'+(i+1);
                    this.main_group.addChild(item);
                }
                item.x = item.anchorOffsetX + 149 * (i % 3);
                item.y =  item.anchorOffsetY + Math.floor((i / 3)) * 143;
                item.show(clone);
                item.visible = true;
            }
            else
            {
                let item = this.main_group.getChildByName('item_'+(index+1));         
                if(item)
                {
                    item.visible = false;
                }
            }
        }
    }

    public clear()
    {
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.clear, this);
        this.timer.removeEventListener(egret.TimerEvent.TIMER, this.update, this);
        this.oldData = null;
        this.randomList = null;
        this.timer.stop();
        this.timer = null;
    }
}